#!/usr/bin/ruby

if ARGV.length < 3
	puts "Error: Missing arguments."
	puts "<server ip> <port> <rcon password>"
	puts "Only use IP addresses, no domain names."
	puts "Eg: ./check.rb 192.168.0.11 27015 ch33s3"
	exit
end

$argv_server_ip = ARGV[0]
$argv_server_port = ARGV[1]
$argv_server_rcon = ARGV[2]

require 'rubygems'
require 'ipaddr'
require 'steam/servers/source_server'

####################################################################################
####################################################################################
# Configuration stuff (Feel free to edit)
####################################################################################
# Path to log file
log_file_loc = 'crash_log.txt' 
# Time in seconds of server check interval.
$check_interval = 6
# Number of times server checked before calculating adverage FPS.
$check_times = 5
# Time in seconds until attempting to reconnect after the server has been restarted.
wait_time = 30
####################################################################################
####################################################################################

# Creates logfile if one doesn't already exist.
if File.exist?(log_file_loc) == false
	File.new(log_file_loc, 'w+')
end

class Checker
	def connect(ip_addr, port, password)
		rcon_connected = false
		while rcon_connected == false
			begin
				ip = IPAddr.new(ip_addr)
				@server = SourceServer.new(ip_addr, port)
				@server.rcon_auth(password)
				@server.rcon_exec("say Ruby Srcds Chcker is now active")
				puts "Connection to " + ip_addr + " established"
				rcon_connected = true
			rescue
				puts "Failed to contact server. Is it running?\nRetrying in 10 sec"
				rcon_connected = false
				sleep 10
			end
		end
	end

	# Same as connect but does not attempt to retry if connection fails.
	def reconnect(ip_addr, port, password)
		rcon_connected = false
		begin
			ip = IPAddr.new(ip_addr)
			@server = SourceServer.new(ip_addr, port)
			@server.rcon_auth(password)
			# Rcon worked
			@server.rcon_exec("say Ruby Srcds Chcker is now active")
			puts "Connection to " + ip_addr + " established"
			rcon_connected = true
		rescue
			puts "Failed to contact server. Is it running?\nRetrying in 5 sec"
			rcon_connected = false
			sleep 5
		end
	end


	def check
		# Loops until the server crashes
		while true
			i = 0
			fps = 0
			rcon_errors = 0
			reset = true
			while i < $check_times
				begin
					stats = @server.rcon_exec('stats').split
					fps = fps + stats[12].to_i
					i = i + 1
					sleep $check_interval
				rescue
					# Resets i variable to allow 6 connection tries
					if reset == true
						i = i - i
						reset_count = false
					end
					puts i
					# This might not be needed (self.reconnect)
					self.reconnect($argv_server_ip, $argv_server_port, $argv_server_rcon)
					fps = fps + 1
					i = i + 1
				end
			end
			fps_adverage = fps / $check_times
			# Server kill comamnds
			if fps_adverage < 40
				puts "Killing srcds at #{Time.now}"
				`killall srcds.exe`
				# Breaks from the check method
				break
			end
		end
	end
end

server = Checker.new
server.connect($argv_server_ip, $argv_server_port, $argv_server_rcon)

while true
	server.check
	puts "Waiting for server to restart"
	sleep wait_time
	# Create a new instance of the connection
	server = Checker.new
	server.connect($argv_server_ip, $argv_server_port, $argv_server_rcon)
end
